/*
 * @Author: LiJia
 * @Date: 2021-03-05 00:47:37
 * @LastEditTime: 2021-04-01 04:19:25
 * @LastEditors: LiJia
 * @Description: Description
 * @FilePath: \class-manage-system\miniprogram\pages\message\main\message.ts
 */
const app = getApp<IAppOption>()
Component({
  properties: {
    clsUserInfo: {
      type: Object,
      value: {}
    }
  },

  data: {
    hasMessageList: false,
    messageList: [],
    _messageList: [],
    searchWord: "",
    emptyTips: `暂时没有新会话\n多多交友也是一种成长呢~`
  },

  // 组件实例化，但节点树还未导入，因此这时不能用setData
  created() {},

  // 节点树完成，可以用setData渲染节点，但无法操作节点
  attached() {
    this.getMessageList()
  },

  // 组件布局完成，这时可以获取节点信息，也可以操作节点
  ready() {},

  // 组件实例被移动到树的另一个位置
  moved() {},

  // 组件实例从节点树中移除
  detached() {},

  methods: {
    // 用户自定义方法
    /**
     * 获取用户消息列表
     */
    getMessageList() {
      let mls = [
        {
          id: 0,
          name: '沈东梁',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 1,
          name: '计科11701班',
          avatarUrl: "https://pic.cnblogs.com/avatar/1754810/20190729093143.png",
          type: 'multiple'
        },
        {
          id: 2,
          name: '鲁智深深深深深深深深深深深深深深深深深深深深深',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 3,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 4,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 5,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 6,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 7,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 8,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 9,
          name: '我也曾风华过，直到当了Coder',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
      ]
      let mlt = [
        {
          id: 0,
          name: '王国栋',
          avatarUrl: 'https://pic.cnblogs.com/avatar/1754810/20190729093143.png',
          type: 'single'
        },
        {
          id: 1,
          name: '网络11701班',
          avatarUrl: "https://pic.cnblogs.com/avatar/1754810/20190729093143.png",
          type: 'multiple'
        }
      ]
      let _ml : any[] = [];

      (app.globalData.clsUserInfo as any).role === 'S' ? _ml = mls : _ml = mlt
      this.setData({
        messageList: [..._ml] as never,
        _messageList: [..._ml] as never,
        hasMessageList: true
      })
    },
    /**
     * 搜索值发生改变触发
     * @param event 
     */
    searchValueChanged(event) {
      this.setData({
        searchWord: event.detail.value
      })
      this.setSearchMsgList(this.data.searchWord)
    },
    /**
     * 根据搜索值来匹配全部会话列表中符合条件的会话，重新渲染会话列表
     * @param searchWord 
     */
    setSearchMsgList(searchWord) {
      if (searchWord.length > 0) {
        let searchResult: never[] = []
        this.data.messageList.forEach(_item => {
          if ((_item as any).name.indexOf(searchWord) !== -1) {
            searchResult.push(_item)
          }
        })
        this.setData({
          _messageList: [...searchResult]
        })
      } else {
        this.setData({
          _messageList: [...this.data.messageList]
        })
      }
    },
    /**
     * 添加新会话
     */
    addNewMessage() {
      console.log('添加新会话')
    }
  }
})

export default {}
